﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using mshtml;
using System.Security.Permissions;

namespace HtmlReader
{
    [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
    public class HtmlDocument
    {
        public HtmlDocument(dynamic domDocument)
        {
            DomDocument = domDocument;
            Dictionary = new Dictionary<int, HtmlElement>();
            foreach (IHTMLElement element in domDocument.all)
                Dictionary.Add(element.GetHashCode(), new HtmlElement(element, this));

            IHTMLElementCollection all = domDocument.all;
            All = new HtmlElementCollection(ref all, this);

            foreach (IHTMLElement element in domDocument.all) 
            {
                if (element.tagName.ToUpper()=="BODY")
                {
                    Body = new HtmlElement(element, this);
                    break;
                }
            }
        }

        internal Dictionary<int, HtmlElement> Dictionary { get; private set; }
        public object DomDocument { get; private set; }
        public HtmlElementCollection All { get; private set; }
        public HtmlElement Body { get; private set; }

        public HtmlElement GetElementById(string id)
        {
            IHTMLDocument3 document = DomDocument as IHTMLDocument3;
            IHTMLElement element = document.getElementById(id);
            if (element == null)
                return null;
            else
                return new HtmlElement(element, this);
        }
    }
}
